import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    currentSong: JSON.parse(window.localStorage.getItem("currentSong")),

    playing: false,
    duration: 0,
    currentTime: 0,
    // 当前歌曲播放的时间

    fav: JSON.parse(window.localStorage.getItem("fav")) || [],
  },
  getters: {
    currentSongUrl: function (state) {
      return state.currentSong
        ? `https://music.163.com/song/media/outer/url?id=${state.currentSong.id}.mp3`
        : null;
    },
  },
  mutations: {
    updateCurrentSong(state, payload) {
      state.currentSong = payload.song;
    },
    updatePlaying(state, payload) {
      state.playing = payload.bool;
    },
    updateDuration(state, payload) {
      state.duration = payload.dur;
    },

    updateCurrentTime(state, payload) {
      state.currentTime = payload.time;
    },

    addFav(state, payload) {
      state.fav.push(payload.id);
    },
  },
  actions: {},
  modules: {},
});
